﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security;
using System.Text;
using System.Security.Cryptography;

namespace WebFlipScreenSaver.Model
{
	[Serializable]
	[DataContract]
	public class WebPageConfigurationViewModel : NotifyPropertyChangedBase
	{
		#region Bindable Properties
		
		private string _title;
		[DataMember]
		public string Title
		{
			get { return _title; }
			set
			{
				_title = value;
				Changed(() => Title);
			}
		}

		private string _webSiteUrl;
		[DataMember]
		public string WebSiteUrl
		{
			get { return _webSiteUrl; }
			set
			{
				_webSiteUrl = value;
				Changed(() => WebSiteUrl);
			}
		}

		private bool _requiresAuth;
		[DataMember]
		public bool RequiresAuth
		{
			get { return _requiresAuth; }
			set
			{
				_requiresAuth = value;
				Changed(() => RequiresAuth);
			}
		}

		private string _siteUserName;
		[DataMember]
		public string SiteUserName
		{
			get { return _siteUserName; }
			set
			{
				_siteUserName = value;
				Changed(() => SiteUserName);
			}
		}

		private string _listDisplayName;

		[DependsUpon("Title")]
		[DependsUpon("WebSiteUrl")]
		public string ListDisplayName
		{
			get
			{
				return string.Format("{0} - {1}", Title, WebSiteUrl);
			}
			set
			{
				_listDisplayName = value;
				Changed(() => ListDisplayName);
			}
		}

		private int _displayTime = 10;
		[DataMember]
		public int DisplayTime
		{
			get { return _displayTime; }
			set
			{
				_displayTime = value;
				Changed(() => DisplayTime);
			}
		}

		private string _sitePassword;
		[DependsUpon("EncryptedSitePasswordBase64")]
		public string SitePassword
		{
			get
			{
				
				return _sitePassword;
			}
			set
			{
				_sitePassword = value;
				Changed(() => SitePassword);
			}
		}


		[DataMember]
		[DependsUpon("SitePassword")]
		public string EncryptedSitePasswordBase64
		{
			get
			{
				if(!string.IsNullOrEmpty(SitePassword))
					return EncryptStringBase64(SitePassword);
				return null;
			}
			set
			{
				if(!string.IsNullOrEmpty(value))
					SitePassword = DecryptBase64EncryptedString(value);
				else
				{
					SitePassword = null;
				}
				Changed(() => EncryptedSitePasswordBase64);
			}
		}

		private string _usernameElementName;
		[DataMember]
		public string UsernameElementName
		{
			get { return _usernameElementName; }
			set
			{
				_usernameElementName = value;
				Changed(() => UsernameElementName);
			}
		}

		private string _passwordElementName;
		[DataMember]
		public string PasswordElementName
		{
			get { return _passwordElementName; }
			set
			{
				_passwordElementName = value;
				Changed(() => PasswordElementName);
			}
		}

		private int _zoomPercent = 100;
		[DataMember]
		public int ZoomPercent
		{
			get { return _zoomPercent; }
			set
			{
				_zoomPercent = value;
				Changed(() => ZoomPercent);
			}
		}

		private int _refreshIntervalSecs = 30;
		[DataMember]
		public int RefreshIntervalSecs
		{
			get { return _refreshIntervalSecs; }
			set
			{
				_refreshIntervalSecs = value;
				Changed(() => RefreshIntervalSecs);
			}
		}


		#endregion
		private string EncryptStringBase64(string value)
		{
			try
			{
				var valueBytes = Encoding.Unicode.GetBytes(value);
				byte[] encryptedBytes = ProtectedData.Protect(valueBytes, null, DataProtectionScope.LocalMachine);
				value = Convert.ToBase64String(encryptedBytes);
			}
			catch (System.Security.Cryptography.CryptographicException ex)
			{

			}

			return value;
		}

		private string DecryptBase64EncryptedString(string base64EncryptedValue)
		{
			string value = string.Empty;
			try
			{
				var encryptedData = Convert.FromBase64String(base64EncryptedValue);
				byte[] decryptedData = ProtectedData.Unprotect(encryptedData, null, DataProtectionScope.LocalMachine);
				value = Encoding.Unicode.GetString(decryptedData);
			}
			catch (System.Security.Cryptography.CryptographicException ex)
			{

			}


			return value;
		}

	}
}
